package com.salesmanager.core.business.book.dao;

import com.salesmanager.core.business.book.Book;
import com.salesmanager.core.business.generic.dao.SalesManagerEntityDaoImpl;
import com.salesmanager.core.business.statistics.model.BookCriteria;
import com.salesmanager.web.model.paging.PaginationDataList;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuTong on 2017/11/21.
 */
@Repository("bookDao")
public class BookDaoImpl extends SalesManagerEntityDaoImpl<Long,Book> implements BookDao {

    @Override
    public PaginationDataList<Book> getBookList(BookCriteria criteria) {
        StringBuilder sql = new StringBuilder();
        StringBuilder countSql = new StringBuilder();
        StringBuilder whereSql = new StringBuilder();

        sql.append("SELECT * FROM BOOK b WHERE 1=1 ");
        countSql.append("SELECT COUNT(*) FROM BOOK b WHERE 1=1 ");

        if (!StringUtils.isBlank(criteria.getBookCode())){
            whereSql.append("AND b.BOOK_CODE = :code ");
        }
        if (!StringUtils.isBlank(criteria.getBookName())){
            whereSql.append("AND b.BOOK_NAME LIKE :name ");
        }

        if (criteria.getBookId() != null){
            whereSql.append("AND b.BOOK_ID= :id ");
        }

        Query query = super.getEntityManager().createNativeQuery(sql.append(whereSql).toString());
        Query countQuery = super.getEntityManager().createNativeQuery(countSql.append(whereSql).toString());

        //分页
        if (criteria.getMaxCount() > 0){
            query.setMaxResults(criteria.getMaxCount());
            query.setFirstResult(criteria.getStartIndex());
        }

        if (!StringUtils.isBlank(criteria.getBookCode())){
            query.setParameter("code", criteria.getBookCode());
            countQuery.setParameter("code", criteria.getBookCode());
        }
        if (!StringUtils.isBlank(criteria.getBookName())){
            query.setParameter("name", "%"+criteria.getBookName()+"%");
            countQuery.setParameter("name", "%"+criteria.getBookName()+"%");
        }

        if (criteria.getBookId() != null){
            query.setParameter("id", criteria.getBookId());
            countQuery.setParameter("id", criteria.getBookId());
        }

        PaginationDataList<Book> result = new PaginationDataList<Book>();
        //总数
        Number totalCount = (Number)countQuery.getSingleResult();
        result.setTotalCount(totalCount.intValue());

        List lists = query.getResultList();
        List<Book> data = new ArrayList<Book>();
        for(int i=0;i<lists.size();i++){
            Object[] o = (Object[]) lists.get(i);
            Book book = new Book();
            book.setId(Long.parseLong(o[0].toString())); //id
            book.setBookName((String) o[4]); //名称-->作者
            book.setStatus((Integer)o[9]);//状态-->描述
            book.setBookCode((String) o[2]);//编码-->名称
            data.add(book);
        }
        result.setData(data);
        return result;
    }
}
